package com.woniu.web.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
//开启swagger
@EnableSwagger2
public class SwaggerConfig {
    @Autowired
    private SwaggerProerties sp;
    //配置主要就是设置Docket对象的一些参数
    @Bean
    public Docket createDocket() {
        //1、添加界面的请求头
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        //header中的token参数非必填，传空也可以
        //token请求头名称，description：请求头带的名字，随意
        tokenPar.name("account_token").description("登录认证token")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build(); //false非必输 true必输
        pars.add(tokenPar.build());

        //常见文档对象，选择文档类型
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        //创建文档对象里面的文档基本信息
        ApiInfo apiInfo = new ApiInfoBuilder()
                .contact(new Contact(sp.getContact().getName(), sp.getContact().getHost(), sp.getContact().getEmail()))
               //接口信息描述
                .description(sp.getDescription())
                //接口的标题
                .title(sp.getTitle())
                .build();
        //把上面的文档对象设置进去以及设置对生成文档的接口路径扫描
        docket.apiInfo(apiInfo)
                .select()
                //扫描控制层
                .apis(RequestHandlerSelectors.basePackage(sp.getBasePackage()))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(pars);
        return docket;
    }
}
